package org.binaryserch;

public class BinarySerchTest {
    public static void main(String[] args) {
        int [] arr = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}  ;
        System.out.println(binarySearch(arr, 13));
        System.out.println(binarySearch(arr, 20));
    }

    /**
     *
     * @param arr 有序数组
     * @param data 查找的数据
     * @return index 数组下标 未查到返回-1
     */

    public static int binarySearch(int[] arr, int data){

        int low = 0 ;

        int height = arr.length-1;

        while(low<=height) {

            int mid = low +(height-low)/2 ;

            if(arr[mid]<data) {
                low = mid+1;
            }else if (arr[mid] == data){
                return mid;
            }else {
                height = mid -1;
            }

        }

        return -1;
    }
}
